package com.huawei.csi.tool;

import com.google.common.eventbus.Subscribe;
import com.huawei.csi.tool.config.GlobalConfig;
import com.huawei.csi.tool.event.AnimFinishedEvent;
import com.huawei.csi.tool.event.EventBusUtil;
import com.huawei.csi.tool.ui.DragScene;
import com.huawei.csi.tool.ui.InitialLoadingPane;
import com.huawei.csi.tool.util.ResourcesUtil;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Parent;
import javafx.scene.PerspectiveCamera;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 主窗口，启动 
 * {@link com.huawei.csi.tool.controller.CJMainController#initialize(URL, ResourceBundle) 一些初始化绑定}
 *
 * @author lxw
 */
@SuppressWarnings("all")
public class CjApp extends Application {
    /**
     * LOGGER
     */
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CjApp.class);
    /**
     * 动画已完成标识
     */
    private boolean animationFinished;
    /**
     * 切换标识
     */
    private boolean rootChanged;
    /**
     * root
     */
    private Parent parent;

    /**
     * 开机动画拖动场景
     */
    private DragScene scene;


    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage rootStage) throws Exception {
        rootStage.setAlwaysOnTop(false); // 初始化不置顶
        EventBusUtil.getEventBus().register(this); // 注册事件
        InitialLoadingPane initialLoadingPane = new InitialLoadingPane(); // 初始化loading画面
        scene = new DragScene(initialLoadingPane, rootStage); // 放到拖动Scense里面
        scene.getStylesheets().add(ResourcesUtil.cssExternalForm("main-stage.css")); // 样式绑定
        loadMainPane(); // 加载主页面
        scene.setFill(Color.TRANSPARENT);
        scene.setCamera(new PerspectiveCamera());
        rootStage.initStyle(StageStyle.TRANSPARENT); // 主题背景虚化
        rootStage.setScene(scene);
        rootStage.setTitle("CJTools");
        rootStage.getIcons().addAll(ResourcesUtil.getIconImages()); // 图标添加
        GlobalConfig.init(rootStage, getHostServices()); // 全局通用参数初始化
        rootStage.show();
    }

    private void loadMainPane() {
        Platform.runLater(() -> {
            EventBusUtil.getEventBus().post(ResourcesUtil.loadFXML("main-page"));
        });
    }


    /***
     * 监听开机动画完成事件
     *
     * @param event 监听的动画完成事件
     **/
    @Subscribe
    public void animFinishedHandler(AnimFinishedEvent event) {
        animationFinished = true;
        if (parent != null && !rootChanged) {
            rootChanged = true;
            resetRoot(parent);
        }
    }

    /**
     * 监听 loadMainPane 完成事件，进行重置
     */
    @Subscribe
    public void mainPaneLoadHandler(Parent parent) {
        this.parent = parent;
        if (animationFinished && !rootChanged) {
            rootChanged = true;
            resetRoot(parent);
        }
    }

    private void resetRoot(Parent parent) {
        Platform.runLater(() -> {
            scene.setRoot(parent);
            scene.setCanDrag();
            scene.getRoot().applyCss();
            scene.getRoot().requestLayout();
            scene.getRoot().layout();
            GlobalConfig.rootStage.setAlwaysOnTop(true);
        });
    }
}
